package com.buer.train.common.plugin.push;

import com.gexin.rp.sdk.base.IAliasResult;
import com.gexin.rp.sdk.base.IPushResult;
import com.gexin.rp.sdk.base.impl.SingleMessage;
import com.gexin.rp.sdk.base.impl.Target;
import com.gexin.rp.sdk.exceptions.RequestException;
import com.gexin.rp.sdk.http.IGtPush;
import com.gexin.rp.sdk.template.NotificationTemplate;
import com.gexin.rp.sdk.template.style.Style6;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.stereotype.Component;

/**
 * @ProjectName: zh_support_api
 * @Package: com.chzu.common.plugin.sms
 * @ClassName: SmsUtils
 * @Description: java类作用描述
 * @Author: lds
 * @CreateDate: 2019/4/16 0016 16:43
 * @UpdateUser: 更新者
 * @UpdateDate: 2019/4/16 0016 16:43
 * @UpdateRemark: 更新说明
 * @Version: 1.0
 */

@Component
@EnableConfigurationProperties(value = PushProperties.class)
public class PushUtils {

    private Logger logger = LoggerFactory.getLogger(PushUtils.class);

    @Autowired
    private PushProperties pushProperties;

    /**
     * 使用系统透传通知
     * @param msg
     * @return
     */
    public NotificationTemplate buildNotificationTemplate(String msg,String text) {

        NotificationTemplate template = new NotificationTemplate();
        template.setAppId(pushProperties.getAppId());
        template.setAppkey(pushProperties.getAppKey());

        // 透传消息设置，1为强制启动应用，客户端接收到消息后就会立即启动应用；2为等待应用启动
        template.setTransmissionType(1);
        template.setTransmissionContent("21");
        // 设置定时展示时间
        // template.setDuration("2015-01-16 11:40:00", "2015-01-16 12:24:00");

        Style6 style = new Style6();
        // 设置通知栏标题与内容
        style.setTitle(msg);
        style.setText(text);
        // 配置通知栏图标
        // 配置通知栏网络图标
        // 设置通知是否响铃，震动，或者可清除
        style.setRing(true);
        style.setVibrate(true);
        style.setClearable(true);
        template.setStyle(style);

        return template;
    }

    public NotificationTemplate buildNotificationTemplateByType(String msg,String text,String scenType) {

        NotificationTemplate template = new NotificationTemplate();
        template.setAppId(pushProperties.getAppId());
        template.setAppkey(pushProperties.getAppKey());

        // 透传消息设置，1为强制启动应用，客户端接收到消息后就会立即启动应用；2为等待应用启动
        template.setTransmissionType(1);
        template.setTransmissionContent("21");
        // 设置定时展示时间
        // template.setDuration("2015-01-16 11:40:00", "2015-01-16 12:24:00");

        Style6 style = new Style6();
        // 设置通知栏标题与内容
        style.setTitle(msg);
        style.setText(text);
        // 配置通知栏图标
        // 配置通知栏网络图标
        // 设置通知是否响铃，震动，或者可清除
        style.setRing(true);
        style.setVibrate(true);
        style.setClearable(true);
        template.setStyle(style);

        template.setTransmissionContent(scenType);

        return template;
    }


    /**
     *
     * @param cidOrAlias
     *            别名或者cid
     * @param msg
     *            透传消息内容
     * @param type
     *            1-cid推，2-别名推
     */
    public boolean pushToSingle(String cidOrAlias, String msg,String text, int type) {
        IGtPush push = new IGtPush("http://sdk.open.api.igexin.com/apiex.htm", pushProperties.getAppKey(), pushProperties.getMasterSecret());
        NotificationTemplate template = buildNotificationTemplate(msg,text);
        SingleMessage message = new SingleMessage();

        // 是否离线推送
        message.setOffline(false);
        // 离线有效时间，单位为毫秒，可选
        message.setOfflineExpireTime(1000 * 600);
        // 消息内容
        message.setData(template);
        // 可选，1为wifi，0为不限制网络环境。根据手机处于的网络情况，决定是否下发
        message.setPushNetWorkType(0);

        Target target = new Target();
        target.setAppId(pushProperties.getAppId());
        if (type == 1) {
            target.setClientId(cidOrAlias);
        } else if (type == 2) {
            // 按别名推送
            target.setAlias(cidOrAlias);
        }
        IPushResult ret = null;
        try {
            ret = push.pushMessageToSingle(message, target);
        } catch (RequestException e) {
            e.printStackTrace();
            // 推送失败时，进行重推
            ret = push.pushMessageToSingle(message, target, e.getRequestId());
        }
        if (ret != null) {
            System.out.println(ret.getResponse().toString());
            if("ok".equals(ret.getResponse().get("result"))){
               return true;
            }

        } else {
            System.out.println("服务器响应异常");
            return false;
        }

        return false;
    }


    /**
     *
     * @param cidOrAlias
     *            别名或者cid
     * @param msg
     *            透传消息内容
     * @param type
     *            1-cid推，2-别名推
     */
    public boolean pushToSingleByType(String cidOrAlias, String msg,String text, int type,String senceType) {
        IGtPush push = new IGtPush("http://sdk.open.api.igexin.com/apiex.htm", pushProperties.getAppKey(), pushProperties.getMasterSecret());
        NotificationTemplate template = buildNotificationTemplateByType(msg,text,senceType);
        SingleMessage message = new SingleMessage();

        // 是否离线推送
        message.setOffline(false);
        // 离线有效时间，单位为毫秒，可选
        message.setOfflineExpireTime(1000 * 600);
        // 消息内容
        message.setData(template);
        // 可选，1为wifi，0为不限制网络环境。根据手机处于的网络情况，决定是否下发
        message.setPushNetWorkType(0);

        Target target = new Target();
        target.setAppId(pushProperties.getAppId());
        if (type == 1) {
            target.setClientId(cidOrAlias);
        } else if (type == 2) {
            // 按别名推送
            target.setAlias(cidOrAlias);
        }
        IPushResult ret = null;
        try {
            ret = push.pushMessageToSingle(message, target);
        } catch (RequestException e) {
            e.printStackTrace();
            // 推送失败时，进行重推
            ret = push.pushMessageToSingle(message, target, e.getRequestId());
        }
        if (ret != null) {
            System.out.println(ret.getResponse().toString());
            if("ok".equals(ret.getResponse().get("result"))){
                return true;
            }

        } else {
            System.out.println("服务器响应异常");
            return false;
        }

        return false;
    }


    /**
     * 绑定方法
     * @param cid
     * @param alias
     */
    public boolean bindAlias(String cid, String alias) {
        IGtPush push = new IGtPush("http://sdk.open.api.igexin.com/apiex.htm", pushProperties.getAppKey(), pushProperties.getMasterSecret());
        IAliasResult bindSCid = push.bindAlias(pushProperties.getAppId(), alias, cid);
        System.out.println("绑定结果：" + bindSCid.getResult() + "错误码:" + bindSCid.getErrorMsg());
        return bindSCid.getResult();
    }


}
